<?php
namespace app\admin\service;

use library\tools\Node;
use think\Db;
use think\facade\Request;
use app\admin\service\CompanyService;
/**
 * 系统合同管理服务
 * Class ContractService
 * @package app\admin\service
 */
class ContractService
{    
    
    /**
     * 检测合同是否存在
     * @param string $id
     * @return boolean
     */
    public static function contract_check($id)
    {
    	$result = Db::name('system_contract')->where(['id'=>$id])->count();
    	if($result){
    		return true;
    	}else{
    		return false;
    	}
    }
    
    
    /**
     * 根据合同ID获取合同基本信息
     * @param string $id
     * @param string $field
     * @return array
     */
    public static function contract_info($id,$field)
    {
    	if($field == '*'){
    		$info = Db::name('system_contract')->where(['id'=>$id])->find();
    	}else{
    		$info = Db::name('system_contract')->where(['id'=>$id])->field($field)->find();
    	}   	
    	if($info){
    		return $info;
    	}else{
    		return [];
    	}
    }
    
    
   
    
    /**
     * 根据合同ID获取险种信息
     * @param string $id
     * @param string $field
     * @return array
     */
    public static function contract_insurance($id,$field)
    {
    	if($field == '*'){
    		$info = Db::name('system_contract_insurance')->where(['contract_id'=>$id])->select();
    	}else{
    		$info = Db::name('system_contract_insurance')->where(['contract_id'=>$id])->field($field)->select();
    	}   	
    	if($info){
    		return $info;
    	}else{
    		return [];
    	}
    }
    
    
	 /**
     * 根据公司ID变更合同编号
     * @param string $id
     * @return array
     */
    public static function contract_code($id)
	{
		$account_type = CompanyService::getAccountType($id);
		$contrat = Db::name('system_contract')->where(['company_id'=>$id,'status'=>[1,2]])->find();
		if($account_type == 1 && $contrat['account_type'] == 2){
			$code = number_rule_max(6);
			$re = Db::name('system_contract')->where(['id'=>$contrat['id']])->update(['code'=>$code,'account_type'=>1]);
			if($re){
				save_number_rule_max(6);
			}
		}
		if($account_type == 2 && $contrat['account_type'] == 1){			
			//获取主公司信息
			$master = CompanyService::getMaster($id);
			$macont = Db::name('system_contract')->where(['company_id'=>$master['id'],'status'=>[1,2]])->find();
			$max = $macont['max'] + 1;
			$code = $macont['code'].'-'.$max;
			$re = Db::name('system_contract')->where(['id'=>$contrat['id']])->update(['code'=>$code,'account_type'=>2]);
			if($re){
				Db::name('system_contract')->where(['id'=>$macont['id']])->update(['max'=>$max]);
			}
		}
	}
	
	
	/**
     * 组合比例
     * @param string $insurance
     * @param string $is_del
     * @return array
     */
    public static function proportion($insurance,$is_del = 0)
    {
    	if(!$is_del){
    		$list = Db::name('system_proportion')->where(['insurance'=>$insurance,'is_deleted'=>0])->select();
    	}else{
    		$list = Db::name('system_proportion')->where(['insurance'=>$insurance])->select();
    	}
    	$arr = [];
    	if($list){
    		foreach($list as $k=>$v){
	    		$arr[$v['id']] = '公司'.$v['company'].'% + 个人'.$v['person'].'%';
	    	}
    	}   	
    	return $arr;
    }
	
  
    /**
     * 导出合同
     * @param array $id 合同ID
     * @return array
     */
    public static function export_contract($id)
    {
    	$date = date('Y-m-d',time());
    	$adate = after_month($date,3);
    	$filename = '合同列表';
		$status = ['1'=>'生效中','2'=>'即将失效','3'=>'解约中','4'=>'已失效'];
    	$titles = array(
    		array('code','合同编号'),
            array('name','公司名称'),
            array('signatory','签约人员'),
            array('sign_date','签约日期'),
            array('start_date','起始日期'),
            array('end_date','截止日期'),
            array('docking_people','对接人员'),
            array('tel','联系电话'),
            array('renewals','续约次数'),
            array('setmeal_type','套餐类型'),
            array('status','状态'),
            array('cancel_date','关闭时间'),
            array('cancel_reason','关闭原因'),
            array('csr_id','所属客服'),
            array('ssr_id','所属社保'),
            array('desc','备注信息'),
        );
    	$list = Db::name('system_contract')->where(['id'=>$id])->order('id desc')->select(); 
		$companyids =  Db::name('system_contract')->where(['id'=>$id])->column('company_id');
		if(!empty($list) && $list){
			$companys = Db::name('system_company')->where(['id'=>$companyids])->column('name','id');
			foreach($list as $k=>$v){
				$arr[$k]['code']  =  $v['code'];		
	    		$arr[$k]['name'] = $companys[$v['company_id']];
	    		$arr[$k]['signatory'] = user_list()[$v['signatory']];
	    		$arr[$k]['sign_date'] = $v['sign_date'];
	    		$arr[$k]['start_date'] = $v['start_date'];
				$arr[$k]['end_date'] = $v['end_date'];
				$arr[$k]['docking_people'] = $v['docking_people'];
				$arr[$k]['tel'] = $v['tel'];
				$arr[$k]['renewals'] = Db::name('system_contract_renewal')->where(['contract_id'=>$v['id']])->count();
				$arr[$k]['setmeal_type'] = ['1' => '点数套餐', '2' => '人数套餐','3' => '尊享人数套餐'][$v['setmeal_type']];
				if($v['status'] == 1){
					if($v['end_date'] && after_accounting($adate,$v['end_date'])){
						$arr[$k]['status'] = '即将到期';
					}else{
						$arr[$k]['status'] = '生效中';
					}					
				}else{
					$arr[$k]['status'] = '已关闭';
				}
				$arr[$k]['cancel_date'] = $v['cancel_date'];
				$arr[$k]['cancel_reason'] = $v['cancel_reason'];
	    		$arr[$k]['csr_id'] = $v['csr_id'] ? user_list()[$v['csr_id']] : '';
	    		$arr[$k]['ssr_id'] = $v['ssr_id'] ? user_list()[$v['ssr_id']] : '';
				$arr[$k]['desc'] = $v['desc'];
	    	}
		}else{
			$arr = [];
		}
        return ['filename'=>$filename,'titles'=>$titles,'list'=>$arr];
    }
    
      
}
